home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cstream.exe / CSDEMO.CPP < prev    next >
C/C++ Source or Header  |  1991-06-07  |  5KB  |  244 lines

  1. /*
  2.     csdemo.cpp
  3.     6-7-91
  4.     class stream demo
  5.  
  6.     Copyright 1991
  7.     John W. Small
  8.     All rights reserved
  9.     Use freely but acknowledge authorship and copyright.
  10.     CIS: 73757,2233
  11.  
  12.     PSW / Power SoftWare
  13.     P.O. Box 10072
  14.     McLean, Virginia 22102 8072
  15.     USA (703) 759-3838
  16.  
  17. */
  18.  
  19.  
  20. #include <string.h>
  21. #include <fstream.h>
  22. #include <cstream.hpp>
  23.  
  24.  
  25.  
  26. #define MAX_STR_BUF  80
  27.  
  28. class Employee : public StreamableClass {
  29.     char *name, *address, *cityStateZip;
  30. static char nameBuf[], addressBuf[], cityStateZipBuf[];
  31.     void construct(char *name, char *address,
  32.         char *cityStateZip)
  33.     {
  34.         this->name = strdup(name);
  35.         this->address = strdup(address);
  36.         this->cityStateZip = strdup(cityStateZip);
  37.     }
  38. public:
  39.     StreamableClassID(Employee,StreamableClass,2);
  40.     Employee(char *name, char *address,
  41.         char *cityStateZip)
  42.         : StreamableClass(CLASS_ID)
  43.     {
  44.         construct(name,address,cityStateZip);
  45.     }
  46.     ~Employee() { delete name; delete address;
  47.         delete cityStateZip; }
  48. };
  49.  
  50. typedef Employee * EmployeE;
  51. #define EmployeE0 ((EmployeE)0)
  52.  
  53. char Employee::nameBuf[MAX_STR_BUF];
  54. char Employee::addressBuf[MAX_STR_BUF];
  55. char Employee::cityStateZipBuf[MAX_STR_BUF];
  56.  
  57. void Employee::store(ostream& os)
  58. {
  59.     os << name << endf << address << endf
  60.         << cityStateZip << endf;
  61. }
  62.  
  63. StreamC Employee::load(istream& is, StreamC C)
  64. {
  65.     if (!C) if ((C = (StreamC) new Employee())
  66.         == StreamC0)
  67.         return C;
  68.     is.getline(nameBuf,MAX_STR_BUF,
  69.         StreamableClass::FieldTermChar);
  70.     is.getline(addressBuf,MAX_STR_BUF,
  71.         StreamableClass::FieldTermChar);
  72.     is.getline(cityStateZipBuf,MAX_STR_BUF
  73.         ,StreamableClass::FieldTermChar);
  74.     ((EmployeE)C)->construct(nameBuf,addressBuf,
  75.         cityStateZipBuf);
  76.     return C;
  77. }
  78.  
  79.  
  80.  
  81. class Product : StreamableClass {
  82.     char *name, * type;
  83.     unsigned price;
  84. static char nameBuf[], typeBuf[];
  85. static unsigned priceBuf;
  86.     void construct(char *name, char *type,
  87.         unsigned price)
  88.     {
  89.         this->name = strdup(name);
  90.         this->type = strdup(type);
  91.         this->price = price;
  92.     }
  93. public:
  94.     StreamableClassID(Product,StreamableClass,3);
  95.     Product(char *name, char *type, unsigned price)
  96.         : StreamableClass(CLASS_ID)
  97.         { construct(name,type,price); }
  98.     ~Product() { delete name; delete type; }
  99. };
  100.  
  101. typedef Product * ProducT;
  102. #define ProducT0 ((ProducT)0)
  103.  
  104. char Product::nameBuf[MAX_STR_BUF];
  105. char Product::typeBuf[MAX_STR_BUF];
  106. unsigned Product::priceBuf;
  107.  
  108. void Product::store(ostream& os)
  109. {
  110.     os << name  << endf << type << endf
  111.         << price << endf;
  112. }
  113.  
  114. StreamC Product::load(istream& is, StreamC C)
  115. {
  116.     if (!C) if ((C = (StreamC) new Product())
  117.         == StreamC0)
  118.         return C;
  119.     is.getline(nameBuf,MAX_STR_BUF
  120.         ,StreamableClass::FieldTermChar);
  121.     is.getline(typeBuf,MAX_STR_BUF
  122.         ,StreamableClass::FieldTermChar);
  123.     is >> priceBuf;
  124.     is.get();  // field term char
  125.     ((ProducT)C)->construct(nameBuf,typeBuf,priceBuf);
  126.     return C;
  127. }
  128.  
  129.  
  130.  
  131. class Special : Product {
  132.     unsigned specialPrice;
  133. static unsigned spriceBuf;
  134.     void construct(unsigned specialPrice)
  135.         { this->specialPrice = specialPrice; }
  136. public:
  137.     StreamableClassID(Special,Product,4);
  138.     Special(char *name, char *type, unsigned price,
  139.         unsigned specialPrice)
  140.         : Product(name,type,price)
  141.     {
  142.         setID(CLASS_ID);
  143.         construct(specialPrice);
  144.     }
  145.     ~Special() {return;}
  146. };
  147.  
  148. typedef Special * SpeciaL;
  149. #define SpeciaL0 ((SpeciaL)0)
  150.  
  151. unsigned Special::spriceBuf;
  152.  
  153. void Special::store(ostream& os)
  154. {
  155.     Product::store(os);
  156.     os << specialPrice << endf;
  157. }
  158.  
  159. StreamC Special::load(istream& is, StreamC C)
  160. {
  161.     if (!C) if ((C = (StreamC) new Special())
  162.         == StreamC0)
  163.         return C;
  164.     Product::load(is,C);
  165.     is >> spriceBuf;
  166.     is.get(); // field term char
  167.     ((SpeciaL)C)->construct(spriceBuf);
  168.     return C;
  169. }
  170.  
  171.  
  172.  
  173.  
  174. StreamableClasses(20);
  175.  
  176.  
  177.  
  178.  
  179. main()
  180. {
  181.  
  182.     RegisterClass(Employee);
  183.     RegisterClass(Product);
  184.     RegisterClass(Special);
  185.  
  186.  
  187.  
  188.     Employee e1("Frank Borland","Sierra Nevada Mts.",
  189.         "California");
  190.     Employee e2("Philippe Kahn","1800 Green Hills Road",
  191.         "Scotts Valley, CA 95067");
  192.     Employee e3("Mike Slater","1700 Green Hills Road",
  193.         "Scotts Valley, CA 95067");
  194.  
  195.     Product  p1("Borland C++","Language",495);
  196.     Product  p2("Quatro Pro 3.0","Business/GUI",99);
  197.     Product  p3("Paradox Engine 2.0","Applications",99);
  198.     
  199.     Special  sp1("Borland C++","Language",495,99);
  200.  
  201.     cout << "Stream three Employee Classes\n" << endl;
  202.     cout << e1 << e2 << e3 << "press enter ... " << flush;
  203.     cin.get(); cout << endl;
  204.     cout << "Stream three Product Classes\n" << endl;
  205.     cout << p1 << p2 << p3 << "press enter ... " << flush;
  206.     cin.get(); cout << endl;
  207.     cout << "Stream one Special Product Class\n" << endl;
  208.     cout << sp1 << "press enter ... " << flush;
  209.     cin.get(); cout << endl;
  210.  
  211.     ofstream oS("emp$prod.cls");
  212.     if (!oS)
  213.         return 1;
  214.  
  215.     oS << p1 << e2 << p3 << e1 << p2 << e3 << sp1;
  216.     oS.close();
  217.  
  218.     ifstream iS("emp$prod.cls");
  219.     if (!iS)
  220.         return 2;
  221.  
  222.     StreamableClass *S1, *S2, *S3;
  223.  
  224.     iS >> S1 >> S2 >> S3;
  225.     cout << "Stream three classes\n" << endl;
  226.     cout << S1 << S2 << S3 << "press enter ... " << flush;
  227.     cin.get(); cout << endl;
  228.     delete S1; delete S2;  delete S3;
  229.  
  230.     iS >> S1 >> S2 >> S3;
  231.     cout << "Stream three more classes\n" << endl;
  232.     cout << S1 << S2 << S3 << "press enter ... " << flush;
  233.     cin.get(); cout << endl;
  234.     delete S1; delete S2; delete S3;
  235.     
  236.     iS >> S1;
  237.     cout << "Stream one last class\n" << endl;
  238.     cout << S1 << "press enter ... " << flush;
  239.     cin.get(); cout << endl;
  240.     delete S1;
  241.  
  242.     return 0;
  243. }
  244.